package com.useeinfo.oa.open.Infrastructure.Interceptor;

import com.useeinfo.framework.sugar.func.codec.Md5Utils;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.MD5Util;
import com.useeinfo.framework.sugar.tools.StringConverters;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @Auther: xudong.sun
 * @Date: 2019/2/27 13:38
 * @Description:
 */
public class CrossDomainCheckInteceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setHeader("Access-Control-Allow-Headers", "Authentication,Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "OPTIONS,GET,POST,DELETE,PUT");

        /**
         * 验证当前时间和配置文件时间间隔
         */
//        String failureTimeStr = Configuration.getConfigurationByName("union");
//        Date failureTime = StringConverters.ToDateOnly(MD5Util.convertMD5(failureTimeStr));
//        if (failureTime.before(new Date())){
//            System.out.println("到期啦。");
//            return false;
//        }

        return true;
    }

}
